from app.models.bazi import *
from app.models.nameList import Lunar
import requests,time
class Naming():
    def __init__(self,date_s,time_s):
        self.date_s = date_s
        self.time_s = time_s
    def bazi(self):
        s = self.date_s.split('-')
        tgdz_data = db.session.query(Lunar.TianGanDiZhiYear,Lunar.TianGanDiZhiMonth,Lunar.TianGanDiZhiDay,Lunar.LYear,Lunar.LunarDateTime).filter_by(GregorianDateTime=self.date_s).first()
        bazi_arr = []
        for item in range(3):
            for s in range(2):
                bazi_arr.append(tgdz_data[item][s])
        word8 = TwelveHour.query.filter_by(timePoint=self.time_s).first().codeName
        word7 = BaziTable.query.filter_by(codeName=word8,dayCode=bazi_arr[4]).first().seventh
        bazi_arr.append(word7)
        bazi_arr.append(word8)
        return {
            'bazi_arr':bazi_arr,
            'shu':tgdz_data.LYear,
            'yl':tgdz_data.LunarDateTime
        }
    def wuxing(self,bz_arr):
        wx_arr = []
        for item in bz_arr:
            wx_word = WuXing.query.filter_by(dayCode=item).first().attriBute 
            wx_arr.append(wx_word)
        return wx_arr
    #计算五行数量
    @staticmethod
    def wx_len(wx):
        wx_len = {
            '金': wx.count('金'),
            '木': wx.count('木'),
            '水': wx.count('水'),
            '火': wx.count('火'),
            '土': wx.count('土'),
        }
        wx_que = []
        wx_xi = []
        wx_ruo = []
        wx_arr = []
        for wx in wx_len:
            wx_arr.append(wx_len[wx]) 
        for item in wx_len:
            if wx_len[item]==0:
                wx_que.append(item)
            if wx_len[item] >2:
                wx_xi.append(item)
            if wx_len[item] <2:
                wx_ruo.append(item)
        
        return {'wx_len':wx_len,'wx_que':wx_que,'wx_xi':wx_xi,'wx_ruo':wx_ruo,'wx_arr':wx_arr}
    #五行分析方法
    def wx_fx(self,bz,wx):
        wx = self.wuxing(bz)
        wx_obj = self.wx_len(wx)
        describe = ''
        wx_xi_hanzi = ''
        wx_que_len =  len(wx_obj['wx_que'])
        wx_xi_len = len(wx_obj['wx_xi'])
        wx_ruo = ','.join(wx_obj['wx_ruo'])
        if wx_que_len == 0 and wx_xi_len == 0:
            describe = '恭喜！！五行齐全，推荐五行属性为[土，火]'
            wx_xi_hanzi = '土'
        if wx_que_len == 0 and not wx_xi_len == 0:
            strs = ','.join(wx_obj['wx_xi'])
            for item in self.wx_nexus:
                if item == wx_obj['wx_xi'][0]:
                    wx_xi_hanzi = self.wx_nexus[item]['ke']
            describe = '五行 [%s] 旺，五行偏弱的属性 [%s]，五行喜 [%s]' % (strs,wx_ruo,wx_xi_hanzi)
        if not wx_que_len == 0 and wx_xi_len == 0:
            strs = ','.join(wx_obj['wx_que'])
            wx_xi_hanzi = wx_obj['wx_que'][0]
            describe = '五行缺 [%s]，五行偏弱的属性 [%s]，五行喜 [%s]' % (strs,wx_ruo,wx_xi_hanzi)

        if not wx_que_len == 0 and not wx_xi_len == 0:
            str1 = ','.join(wx_obj['wx_que'])
            str2 = ','.join(wx_obj['wx_xi'])
            wx_xi_hanzi = wx_obj['wx_que'][0]
            describe = '五行缺 [%s]，五行 [%s] 旺，五行偏弱的属性有 [%s]，五行喜 [%s]' % (str1,str2,wx_ruo,wx_xi_hanzi)
        fx_result={
            'xihao':wx_xi_hanzi,
            'describe':describe,
            'wx_arr':wx_obj['wx_arr']
        }
        return fx_result